/*
 * @Description: 用户实时单提示框
 * @version: 
 * @Company: C20出行
 * @Author: ZhengZhuang
 * @Date: 2019-08-20 18:46:09
 * @LastAuthor   : zhengzhuang
 * @lastTime     : 2019-12-30 14:25:12
 */
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:C20Driver/routers/navigator_util.dart';
import 'package:C20Driver/service/request.dart';
import 'package:C20Driver/utils/flutter_tts.dart';
import 'package:C20Driver/widget/stepper.dart';

class UserRealtimeOrder extends StatefulWidget {
  UserRealtimeOrder({this.realTimeOrderData});
  final realTimeOrderData;

  @override
  _UserRealtimeOrderState createState() => _UserRealtimeOrderState();
}

class _UserRealtimeOrderState extends State<UserRealtimeOrder> {
  Timer _timer;
  int _countdownTime = 10;

  @override
  void initState() {
    super.initState();
    startCountdownTimer();
  }

  void startCountdownTimer() {
    const oneSec = const Duration(seconds: 1);

    var callback = (timer) => {
          setState(() {
            if (_countdownTime < 1) {
              _timer.cancel();
              _goToPage();
              // NavigatorUtil.goBack(context);
            } else {
              _countdownTime = _countdownTime - 1;
            }
          })
        };

    _timer = Timer.periodic(oneSec, callback);
  }

  // 关闭并进入去接乘客页
  _goToPage() {
    Request.goToPage(
            passengerId: widget.realTimeOrderData.passengerId,
            orderId: widget.realTimeOrderData.orderId)
        .then((value) {
      _timer.cancel();
      Navigator.of(context).pop();
      NavigatorUtil.jump(context,
          '/pickUpPassengers?orderId=${widget.realTimeOrderData.orderId}');
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color.fromARGB(100, 0, 0, 0),
      body: Center(
        child: Container(
          width: ScreenUtil().setWidth(600.0),
          height: ScreenUtil().setWidth(820.0),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(ScreenUtil().setWidth(6.0)),
          ),
          child: Column(
            children: <Widget>[
              Expanded(
                child: Column(
                  children: <Widget>[
                    SizedBox(
                      height: ScreenUtil().setWidth(20.0),
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Image.asset(
                          'assets/icon/messageDefaultLogo.png',
                          width: ScreenUtil().setWidth(80.0),
                          height: ScreenUtil().setWidth(80.0),
                        ),
                        SizedBox(
                          width: ScreenUtil().setWidth(10.0),
                        ),
                        Text(
                          '当前 尾号${widget.realTimeOrderData.lastNum}的乘客',
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: ScreenUtil().setSp(32.0),
                            fontWeight: FontWeight.w700,
                          ),
                        ),
                      ],
                    ),
                    SizedBox(
                      height: ScreenUtil().setWidth(50.0),
                    ),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Container(
                              // 圆和线
                              height: ScreenUtil().setWidth(60.0),
                              child: LeftLineWidget(false, true, true,
                                  Color.fromARGB(255, 36, 142, 247)),
                            ),
                            Expanded(
                              child: Container(
                                padding: EdgeInsets.only(
                                    top: ScreenUtil().setWidth(19.0)),
                                child: Text(
                                  '乘客位置',
                                  style: TextStyle(
                                    fontSize: ScreenUtil().setSp(24.0),
                                    color: Colors.black54,
                                  ),
                                  overflow: TextOverflow.ellipsis,
                                ),
                              ),
                            )
                          ],
                        ),
                        Container(
                          decoration: BoxDecoration(
                            border: Border(
                              left: BorderSide(
                                  width: ScreenUtil().setWidth(2.0),
                                  color: Color.fromARGB(255, 215, 215, 215)),
                            ),
                          ),
                          margin: EdgeInsets.only(
                              left: ScreenUtil().setWidth(55.5)),
                          padding: EdgeInsets.fromLTRB(22, 0, 16, 16),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Container(
                                height: ScreenUtil().setWidth(132.0),
                                child: Text(widget.realTimeOrderData.depAre,
                                    maxLines: 3,
                                    overflow: TextOverflow.ellipsis,
                                    style: TextStyle(
                                      fontSize: ScreenUtil().setSp(34.0),
                                    )),
                              )
                            ],
                          ),
                        )
                      ],
                    ),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Container(
                                // 圆和线
                                height: ScreenUtil().setWidth(50.0),
                                child: LeftLineWidget(true, false, false,
                                    Color.fromARGB(255, 255, 102, 0))),
                            Expanded(
                              child: Container(
                                padding: EdgeInsets.only(top: 4),
                                child: Text('目的地位置',
                                    style: TextStyle(
                                      fontSize: ScreenUtil().setSp(24.0),
                                      color: Colors.black54,
                                    ),
                                    overflow: TextOverflow.ellipsis),
                              ),
                            )
                          ],
                        ),
                        Container(
                          decoration: BoxDecoration(
                            border: Border(
                                left: BorderSide(
                                    width: 2, color: Colors.transparent)),
                          ),
                          margin: EdgeInsets.only(
                              left: ScreenUtil().setWidth(55.5)),
                          padding: EdgeInsets.fromLTRB(22, 0, 16, 16),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Container(
                                height: ScreenUtil().setWidth(132.0),
                                child: Text(widget.realTimeOrderData.destArea,
                                    maxLines: 3,
                                    overflow: TextOverflow.ellipsis,
                                    style: TextStyle(
                                      fontSize: ScreenUtil().setSp(34.0),
                                    )),
                              )
                            ],
                          ),
                        )
                      ],
                    ),
                  ],
                ),
              ),
              Text(
                '您距离乘客0.5公里，预计2分钟',
                style: TextStyle(
                  fontSize: ScreenUtil().setSp(26.0),
                  color: Color.fromARGB(255, 255, 153, 0),
                ),
              ),
              SizedBox(
                height: ScreenUtil().setWidth(10.0),
              ),
              MaterialButton(
                child: Text(
                  '去接乘客  ${_countdownTime}秒',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: ScreenUtil().setSp(36.0),
                  ),
                ),
                color: Color.fromARGB(255, 255, 153, 0),
                minWidth: ScreenUtil().setWidth(500.0),
                height: ScreenUtil().setWidth(90.0),
                onPressed: () => _goToPage(),
              ),
              SizedBox(
                height: ScreenUtil().setWidth(50.0),
              ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    if (_timer != null) {
      _timer.cancel();
    }
    VoiceBroadCasting.stopSpeech();
    super.dispose();
  }
}
